package club.justwill.single.Controller;

import club.justwill.single.Service.OrderService;
import club.justwill.single.aspect.LoginRequired;
import club.justwill.single.assembler.OrderAssemble;
import club.justwill.single.assembler.OrderItemAssemble;
import club.justwill.single.assembler.ProjectAssemble;
import club.justwill.single.bean.*;
import club.justwill.single.impl.ToolsImpl;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.persistence.dao.ProjectDao;
import club.justwill.single.persistence.jpo.OrderItem;
import club.justwill.single.persistence.jpo.Orders;
import club.justwill.single.persistence.jpo.Projects;
import club.justwill.single.persistence.support.OrdersBase;
import club.justwill.single.util.JsonResponseHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by jaylon on 15-12-26.
 */
@RestController
@Scope(WebApplicationContext.SCOPE_SESSION)
public class OrderController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderAssemble orderAssemble;


    @LoginRequired
    @RequestMapping(value = URL_ORDER_CREATE, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse createOrders(@RequestBody OrderBean orderBean){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        Orders order = orderService.saveOrder(token,obj,orderBean);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(orderAssemble.assembleOrder(order));
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ORDER_GET, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse getOrders(){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        List<OrdersResp> ordersRespList=orderService.getOrders(token,obj);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(ordersRespList);
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ORDER_PAY, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse payOrder(@RequestBody PayOrder payOrder){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        orderService.payOrder(token,obj,payOrder);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ORDER_PAY_ACTIVITY, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse payActivity(@PathVariable("orderId") long orderId){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        orderService.payActivity(token,obj,orderId);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ORDER_CONFIRM_RECEIPT, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse confirmReceipt(@PathVariable("orderId") long orderId,@RequestBody ConfirmReceipt confirmReceipt){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        orderService.confirmReceipt(token,obj,confirmReceipt,orderId);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        }else {
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ORDER_CONFIRM_RECEIPT_BY_SELLER, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse sellerSetconfirmReceipt(@PathVariable String serialNumber) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        orderService.sellerSetconfirmReceipt(token, obj, serialNumber);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        }else {
            obj.setSuccessful();
        }
        return obj;
    }

    @RequestMapping(value = URL_PROJECT_ORDERS_SEARCH, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchOrdersByProjectId(@RequestBody SearchOrder searchOrder, @PathVariable long projectId){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        List<Orders> ordersList = orderService.searchOrdersByProjectId(token,projectId,searchOrder,obj);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(orderAssemble.assembleOrderList(ordersList));
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_LOGISTIC_ORDER, method = RequestMethod.POST,produces = CoreConstants.MIME_JSON)
    public JsonResponse saveLogisticOrder(@RequestBody Logististic logistic){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        orderService.saveLogisticOrder(token,logistic,obj);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ORDER_DELETE, method = RequestMethod.POST,produces = CoreConstants.MIME_JSON)
    public JsonResponse deleteOrder(@PathVariable long orderId) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        orderService.deleteOrder(token, obj, orderId);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }


    @LoginRequired
    @RequestMapping(value = URL_ORDER_GET_NOT_PAID, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse getNotPaidOrder() {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        List<OrdersResp> ordersList = orderService.getNotPaidOrder(obj, token);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(ordersList);
            obj.setSuccessful();
        }
        return obj;
    }


}
